package cn.sc.summer.adapter.controller;

import cn.hutool.core.lang.tree.Tree;
import cn.sc.summer.constant.model.PageResult;
import cn.sc.summer.constant.model.Result;
import cn.sc.summer.domain.user.SysUserInfoService;
import cn.sc.summer.protocol.ValidatedGroup;
import cn.sc.summer.protocol.form.SysRegistrationForm;
import cn.sc.summer.protocol.form.user.SysUserInfoPageForm;
import cn.sc.summer.protocol.form.user.SysUserInfoUpdateForm;
import cn.sc.summer.protocol.form.user.UserChangePasswordForm;
import cn.sc.summer.protocol.form.user.UserRoleForm;
import cn.sc.summer.protocol.vo.UserInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 类名：用户信息管理
 *
 * @author a-xin
 * @date 2023/8/9 14:01
 */
@Slf4j
@RestController
@RequestMapping("/user")
@Api(tags = "user-用户信息管理")
public class SysUserInfoController {

    @Resource
    private SysUserInfoService sysUserInfoService;

    @PostMapping("/registrationWeb")
    @ApiOperation(value = "用户注册WEB")
    public Result<UserInfoVO> registrationWeb(@Validated @RequestBody SysRegistrationForm form) {
        return Result.success(sysUserInfoService.registrationWeb(form));
    }

    @PostMapping("/registrationApp")
    @ApiOperation(value = "用户注册APP")
    public Result<UserInfoVO> registrationApp(@Validated @RequestBody SysRegistrationForm form) {
        return Result.success(sysUserInfoService.registrationApp(form));
    }

    @PostMapping("/getAllUsers")
    @ApiOperation(value = "获取所有用户信息")
    public Result<PageResult<UserInfoVO>> getAllUsers(@RequestBody SysUserInfoPageForm form) {
        return Result.success(sysUserInfoService.getAllUsers(form));
    }

    @PostMapping("/userBindRole")
    @ApiOperation(value = "用户绑定角色")
    public Result<Boolean> userBindRole(@Validated @RequestBody UserRoleForm form) {
        return Result.success(sysUserInfoService.userBindRole(form));
    }

    @PostMapping("/userUnBindRole")
    @ApiOperation(value = "用户取消绑定角色")
    public Result<Boolean> userUnBindRole(@Validated @RequestBody UserRoleForm form) {
        return Result.success(sysUserInfoService.userUnBindRole(form));
    }

    @GetMapping("/getUserInfo")
    @ApiOperation(value = "获取登录用户信息")
    public Result<UserInfoVO> getUserInfo() {
        return Result.success(sysUserInfoService.getUserInfo());
    }

    @GetMapping("/getUserRoute")
    @ApiOperation(value = "获取登录用户路由信息")
    public Result<List<Tree<String>>> getUserRoute() {
        return Result.success(sysUserInfoService.getUserRoute());
    }

    @GetMapping("/deleteUser")
    @ApiOperation(value = "删除用户")
    public Result<String> deleteUser(@RequestParam("id") String id) {
        sysUserInfoService.deleteUser(id);
        return Result.success();
    }

    @PostMapping("/updateUser")
    @ApiOperation(value = "更新用户信息")
    public Result<UserInfoVO> updateUser(@Validated @RequestBody SysUserInfoUpdateForm form) {
        return Result.success(sysUserInfoService.updateUser(form));
    }

    @PostMapping("/forgetPassword")
    @ApiOperation(value = "忘记密码")
    public Result<String> forgetPassword(@Validated(ValidatedGroup.FORGET_PASSWORD.class) @RequestBody UserChangePasswordForm form) {
        return Result.success(sysUserInfoService.forgetPassword(form));
    }

    @PostMapping("/changePassword")
    @ApiOperation(value = "修改密码")
    public Result<String> changePassword(@Validated @RequestBody UserChangePasswordForm form) {
        return Result.success(sysUserInfoService.changePassword(form));
    }

}
